<template>
  <div class="main-content">
    <div class="data-content">
      <div class="table-content">
        <div class="table-title">
          <div class="title-text">班级列表</div>
          <div class="title-btn">
            <el-button type="primary" @click="addClick">新增</el-button>
            <el-button type="danger" @click="delClick()" :disabled="selRows.length <= 0">批量删除</el-button>
          </div>
        </div>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandler"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="banjimingcheng"
            header-align="center"
            align="center"
            label="班级名称">
          </el-table-column>
          <el-table-column
            prop="renshu"
            header-align="center"
            align="center"
            label="班级人数">
          </el-table-column>
          <el-table-column
            prop="banzhuren"
            header-align="center"
            align="center"
            label="班主任">
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
            <template #default="scope">
              <el-button type="text" size="small" @click="editClick(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="delClick(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper"
          class="pagination-content">
        </el-pagination>
      </div>
    </div>
    <formModel ref="formRef" @formModelChange="formModelChange"></formModel>
  </div>
</template>

<script>
import formModel from './formModel.vue'

export default {
  components: {
    formModel
  },
  data () {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      selRows: []
    }
  },
  activated () {
    this.getDataList()
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      let params = {
        'page': this.pageIndex,
        'limit': this.pageSize,
        'sort': 'id',
        'order': 'desc'
      }
      this.$http({
        url: '/banji/list',
        method: 'get',
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandler (val) {
      this.selRows = val
    },
    // 新增
    addClick () {
      this.$refs.formRef.init()
    },
    // 修改
    editClick (id) {
      this.$refs.formRef.init(id, 'edit')
    },
    // 删除
    delClick (id) {
      var ids = id ? [id] : this.selRows.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/banji/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 表单提交成功回调
    formModelChange () {
      this.getDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.main-content {
  padding: 20px;
  .data-content {
    background-color: #fff;
    border-radius: 4px;
    padding: 20px;
    .table-content {
      .table-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        .title-text {
          font-size: 18px;
          font-weight: bold;
        }
      }
      .pagination-content {
        margin-top: 20px;
        text-align: right;
      }
    }
  }
}
</style> 